% Assumes that groups.dat defines the areas you want to see.
% Must be in the directory in which the simulation was run.
% If type_name is {}, then it will plot a central neuron from each area.
% function plot_net_area(area_name, type_name, num_procs);
function plot_net_num_synapses(area_name, type_name, num_procs);

connectfile='connect.dat';
[a b c d]=textread(connectfile,'%f%f%f%f%*[^\n]','delimiter','\t');
connect=[a b d]; % x y coord of area & d is cell type index.


read_groups; % Assumes groups.dat has the neural area data


index = [];

neuron_num = [];

if isempty(type_name)
    % display a central neuron for each group that is in the same file,
    % 0-X.syn, which will be on the startup processor.

    k=1;
    for i=1:length(areas)
        if strcmp( deblank(areas{i}), area_name) 
            middle_neuron_num = floor((neuron_id(i,1)+neuron_id(i,2))/2);
            % make sure it is on the same proc.
            middle_neuron_num = middle_neuron_num  - mod(middle_neuron_num, num_procs);
            neuron_num = [neuron_num middle_neuron_num];
            cell_type_name{k} = deblank(celltype{i});
            k=k+1;
        end
    end
    if isempty(neuron_num)
        disp([area_name ' is not in groups.dat.']);
        return;
    end
    
else
    for i=1:length(areas)
        if strcmp( deblank(areas{i}), area_name) && strcmp( type_name, deblank(celltype{i}))
            index = i
            break;
        end
    end
    if isempty(index)
        disp([area_name ' ' type_name ' is not in groups.dat.']);
        return;
    end
    
    neuron_num = neuron_id(index,1):num_procs:neuron_id(index,2)
    for i = 1:length(neuron_num)
        cell_type_name{i}= deblank(celltype{index});
    end
end

% all neuron_num values were on the same processor and are in the same .syn
% file, therefore.

    zlabels = '';
    for i=1:length(areas)
        if strcmp( deblank(areas{i}), area_name) 
            zlabels = [zlabels deblank(celltype{i}) '|'];
        end
    end




synfile = [ num2str(mod(neuron_num(1),num_procs)) '-' num2str(neuron_id(end,end)+1) '.syn'];

X=load(synfile);



for kk=1:length(neuron_num)
    figure(kk)
    ind=find(X(:,1)==neuron_num(kk));
    length(ind)
    unique_pre = unique( X(ind,2) );
    
    hold off
    for ii=1:length(ind)
        n= X(ind(ii),4);
        
        xyz0=connect(X(ind(ii),2)+1,:);
        if(n < 1)
            color = 'm';
        elseif n<2
            color = 'b';
        elseif n<3
            color = 'c';
        elseif n<4
            color = 'g';
        elseif n<6
            color = 'y';
        elseif n<8
            color = 'k';
        else
            color = 'r';
        end
        plot3(xyz0(:,1),xyz0(:,2),xyz0(:,3)+1,[color '.']);

        hold on
    end;
    xyz1=connect(X(ind(1),1)+1,:);
    plot3(xyz1(:,1),xyz1(:,2),xyz1(:,3)+1,'r*');
    
    title(['Neurons connected to ' area_name cell_type_name{kk} 'cell #: * ', num2str(neuron_num(kk))], 'FontSize',14);
    set(gca,'zlim',[1 23],'ZTick',[1:23],'ZTickLabel',zlabels);
    pause;
    clear ind
end;





  